{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a6bd35c8-649d-4831-a3c6-da9422a5a64c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-27T03:27:41.639145Z",
     "iopub.status.busy": "2022-05-27T03:27:41.638756Z",
     "iopub.status.idle": "2022-05-27T03:27:42.164672Z",
     "shell.execute_reply": "2022-05-27T03:27:42.164009Z",
     "shell.execute_reply.started": "2022-05-27T03:27:41.639076Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "43388bde-63d6-400e-8f5f-0678a3ffa582",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-25T08:15:00.058469Z",
     "iopub.status.busy": "2022-05-25T08:15:00.058094Z",
     "iopub.status.idle": "2022-05-25T08:15:00.075161Z",
     "shell.execute_reply": "2022-05-25T08:15:00.074403Z",
     "shell.execute_reply.started": "2022-05-25T08:15:00.058443Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A  1    0.036606\n",
       "   2    0.953741\n",
       "   3   -0.456839\n",
       "B  1   -2.153045\n",
       "   2   -0.626984\n",
       "C  1    1.436184\n",
       "   3   -0.672121\n",
       "D  2   -0.196640\n",
       "   3    0.347782\n",
       "dtype: float64"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建实验数据\n",
    "data = pd.Series(\n",
    "    np.random.randn(9),\n",
    "    index=[[\"A\", \"A\", \"A\", \"B\", \"B\", \"C\", \"C\", \"D\", \"D\"], [1, 2, 3, 1, 2, 1, 3, 2, 3]],\n",
    ")\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ceab9c7-81a9-4e66-94f5-14672c72e358",
   "metadata": {},
   "source": [
    "## 一、索引操作（回顾层级索引）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afae85c6-2892-4379-84ab-97c3f3ba6816",
   "metadata": {},
   "source": [
    "### 1.1 外层索引操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2ed06a50-eaff-4696-b4e9-2265daa2f604",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-25T08:23:51.207870Z",
     "iopub.status.busy": "2022-05-25T08:23:51.207553Z",
     "iopub.status.idle": "2022-05-25T08:23:51.215153Z",
     "shell.execute_reply": "2022-05-25T08:23:51.214335Z",
     "shell.execute_reply.started": "2022-05-25T08:23:51.207846Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    0.036606\n",
       "2    0.953741\n",
       "3   -0.456839\n",
       "dtype: float64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 单个索引取值\n",
    "data.loc[\"A\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "548097ba-9a89-41aa-b62c-b6451d7012dd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-25T08:26:35.574067Z",
     "iopub.status.busy": "2022-05-25T08:26:35.573646Z",
     "iopub.status.idle": "2022-05-25T08:26:35.582655Z",
     "shell.execute_reply": "2022-05-25T08:26:35.581445Z",
     "shell.execute_reply.started": "2022-05-25T08:26:35.574021Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "C  1    1.436184\n",
       "   3   -0.672121\n",
       "D  2   -0.196640\n",
       "   3    0.347782\n",
       "dtype: float64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 索引切片\n",
    "data.loc[\"C\":\"D\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d647add7-aa5b-45fb-bde3-6b9e48a67082",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-25T08:26:55.057039Z",
     "iopub.status.busy": "2022-05-25T08:26:55.056646Z",
     "iopub.status.idle": "2022-05-25T08:26:55.065534Z",
     "shell.execute_reply": "2022-05-25T08:26:55.064335Z",
     "shell.execute_reply.started": "2022-05-25T08:26:55.057012Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A  1    0.036606\n",
       "   2    0.953741\n",
       "   3   -0.456839\n",
       "C  1    1.436184\n",
       "   3   -0.672121\n",
       "dtype: float64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 多个索引取值\n",
    "data.loc[[\"A\", \"C\"]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0ee6921-075d-4ee7-99fc-240800e1951d",
   "metadata": {},
   "source": [
    "### 1.2 内层索引操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "cfb90ba2-087b-4149-afb0-3920e37698a0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-25T08:31:13.430698Z",
     "iopub.status.busy": "2022-05-25T08:31:13.430354Z",
     "iopub.status.idle": "2022-05-25T08:31:13.436998Z",
     "shell.execute_reply": "2022-05-25T08:31:13.436266Z",
     "shell.execute_reply.started": "2022-05-25T08:31:13.430671Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9537408534311604"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 同时使用内外层索引\n",
    "data.loc[\"A\", 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bc4c7aff-efdb-413f-ac78-dd09d2622a98",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-25T08:31:28.681764Z",
     "iopub.status.busy": "2022-05-25T08:31:28.681329Z",
     "iopub.status.idle": "2022-05-25T08:31:28.689629Z",
     "shell.execute_reply": "2022-05-25T08:31:28.688645Z",
     "shell.execute_reply.started": "2022-05-25T08:31:28.681737Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    0.953741\n",
       "B   -0.626984\n",
       "D   -0.196640\n",
       "dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 忽略外层索引\n",
    "data.loc[:, 2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0c44702-4c28-40c7-b1ac-0e2c1529f56c",
   "metadata": {},
   "source": [
    "## 二、层次化索引操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3722da23-3dec-4548-abd3-b6e2f744c263",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-27T03:27:49.584413Z",
     "iopub.status.busy": "2022-05-27T03:27:49.584017Z",
     "iopub.status.idle": "2022-05-27T03:27:49.602808Z",
     "shell.execute_reply": "2022-05-27T03:27:49.602035Z",
     "shell.execute_reply.started": "2022-05-27T03:27:49.584395Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>7</td>\n",
       "      <td>one</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>one</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>one</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>two</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>two</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>two</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>two</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   a  b    c  d\n",
       "0  0  7  one  0\n",
       "1  1  6  one  1\n",
       "2  2  5  one  2\n",
       "3  3  4  two  0\n",
       "4  4  3  two  1\n",
       "5  5  2  two  2\n",
       "6  6  1  two  3"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建实验数据\n",
    "data = pd.DataFrame(\n",
    "    {\n",
    "        \"a\": np.arange(7),\n",
    "        \"b\": np.arange(7, 0, -1),\n",
    "        \"c\": [\"one\", \"one\", \"one\", \"two\", \"two\", \"two\", \"two\"],\n",
    "        \"d\": [0, 1, 2, 0, 1, 2, 3],\n",
    "    }\n",
    ")\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e7ae2051-dd3e-4863-ae01-1b057c4d213e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-27T03:27:50.920098Z",
     "iopub.status.busy": "2022-05-27T03:27:50.919772Z",
     "iopub.status.idle": "2022-05-27T03:27:50.928952Z",
     "shell.execute_reply": "2022-05-27T03:27:50.928318Z",
     "shell.execute_reply.started": "2022-05-27T03:27:50.920073Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>0</td>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     a  b  d\n",
       "c           \n",
       "one  0  7  0\n",
       "one  1  6  1\n",
       "one  2  5  2\n",
       "two  3  4  0\n",
       "two  4  3  1\n",
       "two  5  2  2\n",
       "two  6  1  3"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 指定某列数据作为行索引\n",
    "data2 = data.set_index([\"c\"])\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5248b779-7391-45c3-a189-7d27c038d984",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-27T03:27:52.058285Z",
     "iopub.status.busy": "2022-05-27T03:27:52.057745Z",
     "iopub.status.idle": "2022-05-27T03:27:52.068087Z",
     "shell.execute_reply": "2022-05-27T03:27:52.067221Z",
     "shell.execute_reply.started": "2022-05-27T03:27:52.058260Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">one</th>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">two</th>\n",
       "      <th>0</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       a  b\n",
       "c   d      \n",
       "one 0  0  7\n",
       "    1  1  6\n",
       "    2  2  5\n",
       "two 0  3  4\n",
       "    1  4  3\n",
       "    2  5  2\n",
       "    3  6  1"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 指定某几列数据做为行索引，即根据数据构造层次索引\n",
    "data3 = data.set_index([\"c\", \"d\"])\n",
    "data3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b5cea970-ca46-426b-a440-56e84afbe8d0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-27T03:27:54.400064Z",
     "iopub.status.busy": "2022-05-27T03:27:54.399734Z",
     "iopub.status.idle": "2022-05-27T03:27:54.409826Z",
     "shell.execute_reply": "2022-05-27T03:27:54.409114Z",
     "shell.execute_reply.started": "2022-05-27T03:27:54.400039Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">one</th>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>7</td>\n",
       "      <td>one</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>one</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>one</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">two</th>\n",
       "      <th>0</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>two</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>two</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>two</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>two</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       a  b    c  d\n",
       "c   d              \n",
       "one 0  0  7  one  0\n",
       "    1  1  6  one  1\n",
       "    2  2  5  one  2\n",
       "two 0  3  4  two  0\n",
       "    1  4  3  two  1\n",
       "    2  5  2  two  2\n",
       "    3  6  1  two  3"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 指定某几列数据做为行索引，保留原数据\n",
    "data4 = data.set_index([\"c\", \"d\"], drop=False)\n",
    "data4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "49c9f352-f1e3-4a43-9d0f-c9f671a3893a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-27T03:28:42.410982Z",
     "iopub.status.busy": "2022-05-27T03:28:42.410657Z",
     "iopub.status.idle": "2022-05-27T03:28:42.421301Z",
     "shell.execute_reply": "2022-05-27T03:28:42.420540Z",
     "shell.execute_reply.started": "2022-05-27T03:28:42.410959Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>one</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>one</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>one</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>two</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>two</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>two</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>two</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     c  d  a  b\n",
       "0  one  0  0  7\n",
       "1  one  1  1  6\n",
       "2  one  2  2  5\n",
       "3  two  0  3  4\n",
       "4  two  1  4  3\n",
       "5  two  2  5  2\n",
       "6  two  3  6  1"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 逆层次化索引操作\n",
    "data3.reset_index()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
